package zozopo.warehouse;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import org.apache.struts2.interceptor.SessionAware;

import zozopo.background.operator.operations.OperatorTB;
import zozopo.background.operator.operations.OperatorlimitedTB;
import zozopo.database.BusiDatabaseManager;
import zozopo.database.DatabaseManager;
import zozopo.flow.comm.Flow;
import zozopo.flow.comm.FlowCode;
import zozopo.flow.operations.RecordlistTB;
import zozopo.warehouse.operations.GoodsOS;
import zozopo.warehouse.operations.GoodsTB;
import zozopo.warehouse.operations.PurchaseOS;
import zozopo.warehouse.operations.PurchaseTB;
import zozopo.warehouse.operations.PurchaselistOS;
import zozopo.warehouse.operations.PurchaselistTB;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;

public class UnconfirmPurchaselistAction extends ActionSupport implements Action,SessionAware{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private static final String limitedCode ="WAREHOUSE.PURCHASELIST.UNLOCK";
	
	//传入参数
	private int id;
	
	//传出参数
	private String operationResult;
	private PurchaselistTB purchaselist;
	private List<PurchaseTB> list;
	private Map<Integer,GoodsTB> goodsInfoMap;
	

	
	
	private Map<Object, Object> session;
	public void setSession(Map arg0) {
		// TODO Auto-generated method stub
		session = arg0;
	}
	public String execute(){
		OperatorTB operator = (OperatorTB)session.get("operatorInfo"); 
		
		DatabaseManager databaseManager = new BusiDatabaseManager();
		Connection conn = databaseManager.getConnection();
		
		PurchaselistOS purchaselistOS = new PurchaselistOS(conn);
		PurchaseOS purchaseOS = new PurchaseOS(conn);
		GoodsOS goodsOS = new GoodsOS(conn);
		
		
				
		purchaselist = purchaselistOS.getObjById(id);
		list = purchaseOS.getListByPurchaselistId(id);
		
		
		
		Flow flow = new Flow(conn);
		RecordlistTB recordlist = flow.getRecordlist(FlowCode.WAREHOUSE_PURCHASELIST, id);
		if(recordlist.getNextvalue_recordlist() != 1){  //最初状态 为0, nextvalue = 1 改变后非1
			operationResult = "进仓单流程状态已经改变,无法取消.如需取消需要把流程取消或把出仓流程回到最初已打出仓单状态!";
			databaseManager.releaseConnection(conn);
			return SUCCESS;
		}
		
		if(!purchaselist.getUnlockemail_purchaselist().equals(operator.getEmail_operator())){  //如果不是操作人
			operationResult = "确认进仓人才能取消确认进仓";
			databaseManager.releaseConnection(conn);
			return SUCCESS;
		}
		
		int sumNum = purchaseOS.sumNumberByPurchaselistId(id);
		int sumReserve = purchaseOS.sumReserveByPurchaselistId(id);
		if(sumNum != sumReserve){ //如果库存不等于进货，表示些进仓单属性已经改变将无法取消
			operationResult = "库存不等于进货，表示些进仓单属性已经改变将无法取消";
			purchaselist = purchaselistOS.getObjById(id);
			list = purchaseOS.getListByPurchaselistId(id);
			return SUCCESS;
		}
		
		/* in Str 组成*/
		String inStr = "(";
		boolean isFirst = true;
	
		for(PurchaseTB purchase:list){
			if(isFirst){
				inStr += purchase.getGoodsid_purchase();
				isFirst = false;
			}else{
				inStr += "," + purchase.getGoodsid_purchase();
			}
		}
		inStr +=")";
		/* in Str 组成  END*/
		
		try {
			conn.setAutoCommit(false);
			purchaselist.setUnlockemail_purchaselist("");
			purchaselist.setUnlockname_purchaselist("");
			
			purchaselistOS.unconfirmPurchaselist(purchaselist);
			purchaseOS.unconfirm(list);
			
			conn.commit();
			conn.setAutoCommit(true);
		  //	operationResult = "";
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		purchaselist = purchaselistOS.getObjById(id);
		
		goodsInfoMap = goodsOS.getMapByInstr(inStr);
		
		databaseManager.releaseConnection(conn);
		
		return SUCCESS;
	}
	
	public void validate() {
		//权限处理
		OperatorTB operator = (OperatorTB)session.get("operatorInfo"); 
	
		if(operator == null){
			addFieldError("errormessage",getText("operator.timeoutornotlogin"));
			return ; 
		} 
		
		//用户权限
		Map<String,OperatorlimitedTB> operatorlimitedMap = (Map<String,OperatorlimitedTB>)session.get("operatorlimitedMap");
		
		if(operatorlimitedMap.get(limitedCode) == null){
			addFieldError("errormessage","用户没有权限.");
			return;
		}
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getOperationResult() {
		return operationResult;
	}
	public void setOperationResult(String operationResult) {
		this.operationResult = operationResult;
	}
	public PurchaselistTB getPurchaselist() {
		return purchaselist;
	}
	public void setPurchaselist(PurchaselistTB purchaselist) {
		this.purchaselist = purchaselist;
	}
	public List<PurchaseTB> getList() {
		return list;
	}
	public void setList(List<PurchaseTB> list) {
		this.list = list;
	}
	public static String getLimitedcode() {
		return limitedCode;
	}
	public Map<Integer, GoodsTB> getGoodsInfoMap() {
		return goodsInfoMap;
	}
	public void setGoodsInfoMap(Map<Integer, GoodsTB> goodsInfoMap) {
		this.goodsInfoMap = goodsInfoMap;
	}

}
